package org.balthie.cal.impl.cmd.spec;

import java.util.Deque;

import org.balthie.cal.core.ICalKind.CalKind;
import org.balthie.cal.common.ex.InputException;
import org.balthie.cal.core.ICommand;

public class PrevCmdTypeSpecs {

	public static final PrevCmdTypeSpec IS_NUMB = new PrevCmdTypeSpec(CalKind.LiterallyNumb);

	public static final PrevCmdTypeSpec IS_CTRL = new PrevCmdTypeSpec(CalKind.Controllor);

	public static final PrevCmdTypeSpec IS_MATHOPT = new PrevCmdTypeSpec(CalKind.MathOperator);

	public static class PrevCmdTypeSpec extends AbsCmdsSpec {

		private CalKind expect;

		private PrevCmdTypeSpec(CalKind literallynumb) {
			this.expect = literallynumb;
		}

		@Override
		protected boolean doCheck(Deque<ICommand> candidate, boolean isThrow) {
			if (candidate.isEmpty())
				return Boolean.FALSE;

			Boolean isSatisfied = expect == candidate.getLast().getCalKind();
			if (!isSatisfied && isThrow) {
				throw new InputException(String.format("ERROR: %s [expect= %s] [actually= %s]",
						this.getClass().getSimpleName(), this.expect, candidate.getLast().getCalKind()));
			} else {
				return isSatisfied;
			}
		}
	}
}
